<template>

	<image class="addbutton" src="../../static/add.png" mode="aspectFit" @tap="getUploadFile"></image>
	<view class="container">
		<view class="dispaly">
			<view v-for="(item,index) in recordList">
				<NewCard :title='item.eatFood' :value='item.heat' :picUrl='item.foodPic' :time='item.createTime'>
				</NewCard>
			</view>
		</view>
	</view>
</template>


<script setup>
	import {
		getRecordList,
		record
	} from '../../api/record.js'

	import {
		uploadFile
	} from '../../api/uploadFile.js'
	import {
		getOpenId,
		doRegister,
		doLogin
	} from '@/api/register.js'

	import {
		ref
	} from 'vue'

	const recordList = ref([]);


	//自动登录
	uni.login({
		success(res) {
			if (res.code) {
				// uni.getUserInfo({
				// 	success: info => {

				// 		console.log(info.userInfo, '[[[]]]');
				// 		//发起网络请求
				// 		getOpenId(res.code).then(res => {

				// 			doLogin({
				// 				"openId": res.msg
				// 			}).then(login => {

				// 				uni.setStorageSync('token', login.token)
				// 				fetchData();
				// 			})


				// 		})
				// 	}
				// })

				doLogin({
					"openId": res.code
				}).then(login => {

					if (login.msg === "用户不存在") {
						uni.showModal({
							title: '提示',
							content: '用户未登录',
							success: function(res) {
								if (res.confirm) {
									console.log('用户点击确定');
									// 用户点击了确定按钮的相关逻辑可以放在这里
								} else if (res.cancel) {
									console.log('用户点击取消');
									// 用户点击了取消按钮的相关逻辑可以放在这里
								}
							}
						});
					} else {
						uni.setStorageSync('token', login.token)
						fetchData();
					}

				})

			} else {
				console.log('登录失败！' + res.errMsg)
			}
		}


	});

	//保存当前记录
	const recordCurrent = async (picUrl) => {
		try {
			const data = await record({
					"foodPic": picUrl
				}

			);
		} catch (error) {
			console.error('获取数据失败：', error);
		}
	};

	const fetchData = async () => {
		try {
			const data = await getRecordList({
					"pageNum": 1,
					"pageSize": 10,
					"data": {}
				}

			);
			recordList.value = data.data;
		} catch (error) {
			console.error('获取数据失败：', error);
		}
	};







	// 拍照并上传文件
	const getUploadFile = () => { // 使用箭头函数
		console.log("测试拍照 ");
		uni.chooseImage({
			success: (chooseImageRes) => {
				const tempFilePaths = chooseImageRes.tempFilePaths;
				console.log(tempFilePaths);

				uploadFile(tempFilePaths[0], 'file')
					.then(result => {
						let data = JSON.parse(result)
						console.log(result.data, 'pdddddddddppppp');
						console.log(JSON.parse(result), 'pppppp');
						console.log("图片上传成功，记录当前饮食");
						record({
							"foodPic": data.data.url,
							"deleted": 0
						}).then(res => {
							fetchData();
						})

					})
					.catch(error => {
						console.error(error);
					});


			}
		});
	};
</script>


<style>
	.container {

		width: 100%;
		height: 100%;
	}

	.addbutton {
		position: fixed;
		align-items: center;
		bottom: 0;
		left: 50%;
		transform: translateX(-50%);
		height: 120rpx;
	}

	.scroll-Y {
		height: 300rpx;
	}

	.scroll-view_H {
		white-space: nowrap;
		width: 100%;
	}

	.scroll-view-item {
		height: 300rpx;
		line-height: 300rpx;
		text-align: center;
		font-size: 36rpx;
	}

	.scroll-view-item_H {
		display: inline-block;
		width: 100%;
		height: 300rpx;
		line-height: 300rpx;
		text-align: center;
		font-size: 36rpx;
	}
</style>